#include "Server.h"

user_list *Init_Sql_List(void)
{
    user_list *list = (user_list *)malloc(sizeof(user_list));
    if (NULL == list)
    {
        printf("创建错误 行 %d 文件 %s \n",__LINE__ , __FILE__);
        return NULL;
    }
    list->len = 0;

    return list;
}

void tail_insert_tail(user_list *list, user_data data)
{
    if (NULL == list)
    {
        printf("传入表为空\n");
        return;
    }

    list->data[list->len] = data;
    list->len++;

    return;
    
}

void pos_del_list(user_list * list,user_data data)
{
    if (NULL == list)
    {
        printf("传入表为空\n");
        return ;
    }
    
    int index = find_Sql_list_index(list ,data);
    if (index == -1)
    {
        printf("用户 %s 不存在\n",data.name);
        return;
    }
    for (size_t i = index; i < list->len -1; i++)
    {
        list->data[i] = list->data[i+1];
    }
    
    list->len-- ;
    
    return;
}

int find_Sql_list(user_list *list , user_data data)
{

    if (NULL == list)
    {
        printf("传入表为空\n");
        return -1;
    }
    
    for (size_t i = 0; i < list->len; i++)
    {
        if (!strcmp(list->data[i].name , data.name))
        {
            if (!strcmp(list->data[i].password , data.password))
            {
                return 1 ;
            }
            
        }
        
    }
    return 0 ;    
}

int find_Sql_list_index(user_list *list , user_data data)
{
    if (NULL == list)
    {
        printf("传入表为空\n");
        return -1 ;
    }
    
    for (size_t i = 0; i < list->len; i++)
    {
        if (!strcmp(list->data[i].name , data.name))
        {
            if (!strcmp(list->data[i].password , data.password))
            {
                return i ;
            }
            
        }
        
    }
    return -1 ;    
}